/*
<license>
Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

This file is part of quicktag <http://code.google.com/p/quicktag/>

quicktag is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

quicktag is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this code.
If not, see <http://www.gnu.org/licenses/>.
</license>
*/
#include "mp4_tag.h"

// from libmp4v2
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include <mp4.h>

#include <generik/stringutils.h>
#include <iostream>

namespace quicktag
{
	
	int read_mp4(const std::string& filename, quicktag::tags& tags)
	{
		/* ???
		#ifdef NEWMP4
		gint pic_num;
		#endif
		*/
		
		MP4FileHandle mp4file = MP4Read(filename.c_str(), 0);
		if (mp4file == MP4_INVALID_FILE_HANDLE)
		{
			//std::cout<<filename<<" does not seem to be an mp4 file"<<std::endl;
			//gchar *filename_utf8 = filename_to_display(filename);
			//Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' (%s)."),filename_utf8,_("MP4 format invalid"));
			//g_free(filename_utf8);
			return READVAL_FILE;
		}

		// Title
		char* title;
		MP4GetMetadataName(mp4file, &title);
		tags.title = generik::eat_string(title);
		
		// Artist
		char* artist;
		MP4GetMetadataArtist(mp4file, &artist);
		tags.artist = generik::eat_string(artist);
		
		// Album
		char* album;
		MP4GetMetadataAlbum(mp4file, &album);
		tags.album = generik::eat_string(album);

		/*
		// Disks and total disks
		char* disk;
		char* disktotal;
		if (MP4GetMetadataDisk(mp4file, &disk, &disktotal))
		{
			free(disk);
			free(disktotal);
		}
		*/

		// Year
		char* year;
		MP4GetMetadataYear(mp4file, &year);
		tags.year = generik::eat_string(year);

		// Track and total track
		u_int16_t tracknr;
		u_int16_t ntracks;
		if (MP4GetMetadataTrack(mp4file, &tracknr, &ntracks))
		{
			tags.tracknr = generik::to_string(tracknr);
			tags.ntracks = generik::to_string(ntracks);
		}
		
		// Genre
		char* genre;
		MP4GetMetadataGenre(mp4file, &genre);
		tags.genre = generik::eat_string(genre);
		
		// Comment
		char* comment;
		MP4GetMetadataComment(mp4file, &comment);
		tags.comment = generik::eat_string(comment);
		
		/*
	    MP4GetMetadataWriter(mp4file, &FileTag->composer);

	     * Encoding Tool *
	    MP4GetMetadataTool(mp4file, &FileTag->encoded_by);

		Unimplemented:
	    Tempo / BPM
	    MP4GetMetadataTempo(file, &string)

	     * Picture *
	#ifdef NEWMP4
	    // There version can handle multiple pictures!
	    // Version 1.6 of libmp4v2 introduces an index argument for MP4GetMetadataCoverart
	    for (pic_num = 0; (MP4GetMetadataCoverArt( mp4file, &coverArt, &coverSize,pic_num )); pic_num++)
	#else
	    // There version handle only one picture!
	    if ( MP4GetMetadataCoverArt( mp4file, &coverArt, &coverSize ) )
	#endif
	    {
		Picture *pic;
		
		pic = Picture_Allocate();
		if (!prev_pic)
		    FileTag->picture = pic;
		else
		    prev_pic->next = pic;
		prev_pic = pic;

		pic->size = coverSize;
		pic->data = coverArt;
		pic->type = PICTURE_TYPE_FRONT_COVER;
		pic->description = NULL;
	    }
	    
	    */


	    /* Free allocated data */
	    MP4Close(mp4file);

	    return READVAL_OK;
	}

}




